package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.text.TextUtils
import android.webkit.WebSettings
import android.widget.FrameLayout
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.mvp.contract.PayInstructionsContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.PayInstructionsPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.CommonUtils
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import kotlinx.android.synthetic.main.activity_enter_protocol.*
import org.jsoup.Jsoup

/**
 * Created by JacobHHH on 2018/4/22.
 * 付款说明
 */
class PayInstructionsActivity : BaseMvpLcecActivity<FrameLayout,String,PayInstructionsContract.Model,PayInstructionsContract.View,PayInstructionsPresenter>(),PayInstructionsContract.View{

    override fun createPresenter(): PayInstructionsPresenter = PayInstructionsPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_pay_instructioons

    override fun initData() {
        super.initData()
        loadData(true)
    }

    override fun initEvent() {
    }

    override fun loadData(refresh: Boolean) {
        presenter.loadData(refresh)
    }

    override fun setData(data: String?) {
        showContent()
        initWebView(data!!)
    }

    private fun initWebView(body : String)  {
        if (!TextUtils.isEmpty(body)) {
            if (CommonUtils.isNetWorkConnected(mContext)) {
                //根据cache-control决定是否从网络上取数据。
                webView.settings.cacheMode = WebSettings.LOAD_DEFAULT
            } else {
                webView.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK//没网，则从本地获取，即离线加载
            }
            webView.settings.loadWithOverviewMode = true// 缩放至屏幕的大小
            webView.loadDataWithBaseURL(null, getNewContent(body), "text/html", "utf-8", null)
        }
    }

    private fun getNewContent(htmlText: String): String {
        val doc = Jsoup.parse(htmlText)
        val elements = doc.getElementsByTag("img")
        for (element in elements) {
            element.attr("width", "100%").attr("height", "auto")
        }
        return doc.toString()
    }
}